require("datastream")

WEATHER={
	["sunny"]={
		Brightness=0,
		Contrast=1,
		Colour=1,
		Precipitation=0,
		SoundLevel=3.9,
		Pitch=100,
	},
	["cloudy"]={
		Brightness=0,
		Contrast=1,
		Colour=0.6,
		Precipitation=0,
		SoundLevel=3.9,
		Pitch=100,
	},
	["rain"]={
		Brightness=-0.07,
		Contrast=0.9,
		Colour=0.4,
		Precipitation=10,
		SoundLevel=3.9,
		Pitch=100,
		Sound="ambient/weather/rumble_rain_nowind.wav"
	},
	["heavyrain"]={
		Brightness=-0.07,
		Contrast=0.9,
		Colour=0.4,
		Precipitation=15,
		SoundLevel=3.9,
		Pitch=100,
		Sound="ambient/weather/rumble_rain_nowind.wav"
	},
	["sunnyrain"]={
		Brightness=0,
		Contrast=0.7,
		Colour=1.2,
		Precipitation=10,
		SoundLevel=3.9,
		Pitch=100,
		Sound="ambient/weather/rumble_rain_nowind.wav"
	},
	["storm"]={
		Brightness=-0.15,
		Contrast=0.7,
		Colour=0.1,
		Precipitation=20,
		SoundLevel=3.9,
		Pitch=100,
		Sound="ambient/weather/rumble_rain.wav"
	},
	["dark"]={
		Brightness=0,
		Contrast=0.3,
		Colour=1,
		Precipitation=0,
		SoundLevel=3.9,
		Pitch=100,
	},
	["darkrain"]={
		Brightness=0,
		Contrast=0.3,
		Colour=1,
		Precipitation=5,
		SoundLevel=3.9,
		Pitch=100,
		Sound="ambient/weather/rumble_rain_nowind.wav"
	},
}

SetGlobalString("weather","sunny")

CreateConVar( "weathermod_enabled", 0, { FCVAR_REPLICATED, FCVAR_ARCHIVE, FCVAR_NOTIFY } ) 

CreateConVar( "weathermod_lightning", 0, { FCVAR_REPLICATED, FCVAR_ARCHIVE, FCVAR_NOTIFY } ) 

CreateConVar( "weathermod_wind", 0, { FCVAR_REPLICATED, FCVAR_ARCHIVE, FCVAR_NOTIFY } ) 

function FireLightning()
	if ( GetConVar("weathermod_enabled"):GetBool() == false) then return end
	if ( GetConVar("weathermod_lightning"):GetBool() == false) then return end
	if not GetGlobalString("weather") or not WEATHER[GetGlobalString("weather")] then return end
	if WEATHER[GetGlobalString("weather")].Precipitation < 15 then return end
	local allents = ents.FindByClass( "prop_physics" )
	if #allents == 0 then return end
	local ent = table.Random( allents )
	if ent:IsValid() then
		datastream.StreamToClients( player.GetAll( ), "LightningEntity", ent );  
		local pos = ent:LocalToWorld(ent:OBBCenter())
		util.BlastDamage( GetWorldEntity(), GetWorldEntity(), pos, 100, math.random( 50, 200 ) )
	else
		FireLightning()
	end
	
end

local LastLightning=0

function DoWindCalc()
	if ( GetConVar("weathermod_enabled"):GetBool() == false) then return end
	if not GetGlobalString("weather") or not WEATHER[GetGlobalString("weather")] then return end
	local Precipitation = WEATHER[GetGlobalString("weather")].Precipitation
	if ( GetConVar("weathermod_wind"):GetBool() == true) then
		if Precipitation < 5 then return end
		local entitys = ents.FindByClass("prop_physics")
		if #entitys == 0 then return end
		for k, v in pairs(entitys) do
			local entphys = v:GetPhysicsObject();
			if entphys:IsValid() then
				entphys:AddVelocity(Vector(math.sin(CurTime()/4)*math.random(0,1),math.sin(CurTime()/4)*math.random(0,1),0))
			end 
		end 
	end
	if CurTime()>LastLightning and Precipitation > 10 then
		if math.random(1,20)==10 then
			FireLightning()
			LastLightning=CurTime()+math.random(45,180)
		end
	end
end

timer.Create( "WindTimer", 0.5, 0, function() DoWindCalc() end )

function SetWeatherMod_Weather( ply, command, args )
	if !ply:IsAdmin() then return end
	if !WEATHER[args[1]] then return end
	SetGlobalString("weather",args[1])
end

concommand.Add( "weathermod_selectweather", SetWeatherMod_Weather ) 


